import { getTagYear, SubjectType } from '@/apis';
import type { HbSubjectParam } from '@/apis/types';
import { transferYearToMultipleSelector } from '@/utils';
import { getAreaConfig, getIndustryConfig, getTagTypeConfig } from '@/utils/filterConfig';
import { type FilterMenuProps, ComponentType, EDatePickerTypes, type IResult } from 'vc';
import { getCompleteState, getEvaluationResult } from '../apis';

export function useScreen() {
  const PAGE_SIZE = 15;

  const queryData = reactive<HbSubjectParam>({
    hbSubjectType: SubjectType.CLEANER_PRODUCTION,
    pageNum: 1,
    pageSize: PAGE_SIZE,
    searchTxt: '',
    sortRule: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    industryCategoryCodeLv1: '',
    industryCategoryCodeLv2: '',
    industryCategoryCodeLv3: '',
    tagTypeCode: ''
  });

  /** 审核结果 */
  function evaluationResult() {
    const { data } = useRequest(getEvaluationResult);

    return computed(() => {
      return {
        title: '审核结果',
        component: ComponentType.MULTIPLE,
        options: data || [],
        submitFn: (data: string) => {
          queryData.evaluationResultCode = data;
        }
      };
    });
  }

  /** 年度 */
  function getYear() {
    const { data } = useRequest(getTagYear, {
      defaultParams: [SubjectType.CLEANER_PRODUCTION]
    });
    const options = computed(() => {
      return transferYearToMultipleSelector(data.value || []);
    });

    return computed(() => {
      return {
        title: '认定年度',
        component: ComponentType.MULTIPLE,
        options: options || [],
        customYear: true,
        submitFn: (data: string) => {
          queryData.tagYear = data;
        }
      };
    });
  }

  /** 完成状态 */
  function completeState() {
    const { data } = useRequest(getCompleteState);

    return computed(() => {
      return {
        title: '完成状态',
        component: ComponentType.MULTIPLE,
        options: data || [],
        submitFn: (data: string) => {
          queryData.completeStateCode = data;
        }
      };
    });
  }

  const screen = ref<FilterMenuProps[]>([
    {
      ...getAreaConfig().value,
      submitFn: (val: IResult) => {
        const { lv1, lv2, lv3 } = val;
        queryData.provinceCode = lv1.join();
        queryData.cityCode = lv2.join();
        queryData.countryCode = lv3.join();
      }
    },
    {
      ...getIndustryConfig().value,
      submitFn: (val: IResult) => {
        const { lv1, lv2, lv3 } = val;
        queryData.industryCategoryCodeLv1 = lv1?.join() || '';
        queryData.industryCategoryCodeLv2 = lv2?.join() || '';
        queryData.industryCategoryCodeLv3 = lv3?.join() || '';
      }
    },
    {
      ...getTagTypeConfig(SubjectType.CLEANER_PRODUCTION).value,
      submitFn: (data: string) => {
        queryData.tagTypeCode = data;
      }
    },
    {
      ...evaluationResult().value
    },
    {
      ...completeState().value
    },
    {
      title: '近五年是否被环保处罚',
      component: ComponentType.SINGLE,
      options: [
        { name: '全部', code: '' },
        { name: '是', code: '1' },
        { name: '否', code: '0' }
      ],
      submitFn: (data) => {
        queryData.isPunishedInRecent5Years = data;
      }
    },
    {
      ...getYear().value
    },
    {
      title: '披露日期',
      component: ComponentType.DATE,
      type: EDatePickerTypes.DATERANGE,
      shortcuts: [365, 365 * 3],
      submitFn: (data: string) => {
        queryData.noticeStartDate = data[0] || '';
        queryData.noticeEndDate = data[1] || '';
      }
    }
  ]);

  return {
    PAGE_SIZE,
    queryData,
    screen
  };
}
